/**
 * Copyright (c) 快宝网络 kuaidihelp.com Co., Ltd. All Rights Reserved 禁止外泄以及用于其它的商业用途
 */

import React, { useRef } from 'react';
import { Button, Space, Popconfirm, message } from 'antd';
import { getProductList, essayDelete } from '@/services/news/product';
import { PublishItem } from '../utils';
import type { ActionType } from '@ant-design/pro-table';
import { history } from 'umi';
import { ProList } from '@ant-design/pro-list';
import styles from './index.less';

const Index = () => {
  const actionRef = useRef<ActionType>();

  const handleDelete = (id: string) => {
    essayDelete({ id }).then((res) => {
      const { code, msg } = res;
      if (code == 0) {
        message.success('删除成功');
        actionRef.current?.reload();
      } else {
        message.error(msg);
      }
    });
  };
  const handleAdd = (id?: string) => {
    const query: {
      id?: string;
    } = {
      id,
    };
    history.push({
      pathname: '/f/news/product/details',
      query,
    });
  };
  const getList = async (values: any) => {
    return getProductList(values).then((res) => {
      const { code, data, msg } = res;
      if (code == 0) {
        const { list, count: total } = data;
        return {
          total,
          data: list,
        };
      } else {
        message.error(msg);
        return {};
      }
    });
  };
  return (
    <ProList
      className={styles.productPublish}
      actionRef={actionRef}
      toolbar={{
        settings: [
          <Button key='3' type='primary' onClick={handleAdd.bind(null, '')}>
            发布
          </Button>,
        ],
      }}
      search={{}}
      rowKey='name'
      request={getList}
      itemLayout='vertical'
      pagination={{
        pageSize: 20,
      }}
      metas={{
        search: {
          fieldProps: {
            placeholder: '请输入标题搜索',
          },
        },
        title: {
          search: false,
        },
        description: {
          search: false,
          render: (_, record: PublishItem) => {
            const { id, update_time } = record;
            return (
              <Space>
                <div>{update_time}</div>
                <a onClick={handleAdd.bind(null, id)}>重新编辑</a>
                <Popconfirm title={`你确定删除吗？`} onConfirm={handleDelete.bind(null, id)}>
                  <a>删除</a>
                </Popconfirm>
              </Space>
            );
          },
        },
      }}
    ></ProList>
  );
};

export default Index;
